#/!bin/bash
echo -e "\033[32m sh install.sh [user] 可以同时安装root和指定多账户，加上第二个参数 \033[0m"

user=$1
root_path="$PWD"
zsh_install_path="$root_path/oh-my-zsh.sh"
vimrc_path="$root_path/vimrc"
zshrc_path="$root_path/zshrc"

# 检查用户是否存在
check_user(){
  user=$1
  id $user >& /dev/null
  if [ $? -ne 0 ]; then
      echo -e "\033[33m User:$user\033[0m\033[31m 用户不存在! \033[0m"
      exit 1;
  fi
}

# 检查zsh是否安装
check_zsh_install(){
  type zsh >/dev/null 2>&1
  if [ $? -ne 0  ]; then 
    echo -e "\033[31m zsh 未安装! \033[0m"
    echo -e "\033[33m 开始安装 zsh! \033[0m"
    yum install zsh -y
    echo -e "\033[32m zsh 安装完成... \033[0m"
  fi
}

# 检查当前用户是不是root
if [ $USER != "root" ]; then
  echo -e "\033[31m 必须用 root 账户运行! \033[0m"
  exit 1
fi

check_zsh_install
echo -e "\033[33m 开始配置zshrc、vimrc! \033[0m"

if [ ! -d "/root/.oh-my-zsh" ]; then
  sh $zsh_install_path
  cp $zshrc_path ~/.zshrc
fi

if [ ! -d "/root/.vim" ]; then
  git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
  cp -r $root_path/colors ~/.vim
  cp $vimrc_path ~/.vimrc
fi
source ~/.zshrc
echo -e "\033[33m 配置完成! \033[0m"

if [ "U$user" == "U" ]; then
    chsh -s /bin/zsh root
    echo -e "\033[32m root 安装配置完成!\033[0m"
    exit;
fi

check_user $user

id $user >& /dev/null
if [ $? -ne 0 ]; then
    echo -e "\033[33m User:$user\033[0m\033[31m not found! \033[0m"
    exit 1;
fi

user_path="/home/$user"
echo -e "\033[32m start copy... \033[0m"
cp -r "/root/.vim" $user_path
cp -r "/root/.vimrc" $user_path
cp -r "/root/.zshrc" $user_path
cp -r "/root/.oh-my-zsh" $user_path

chown -R $user:$user "$user_path/.vim"
chown -R $user:$user "$user_path/.vimrc"
chown -R $user:$user "$user_path/.zshrc"
chown -R $user:$user "$user_path/.oh-my-zsh"
echo -e "\033[32m done... \033[0m"

echo -e "\033[33m start chooes zsh! \033[0m"
#su - ${user} <<HERE
#    chsh -s /bin/zsh
#HERE
chsh -s /bin/zsh $user
echo -e "\033[32m insert finish!\033[0m"